package com.diver.flinkdemo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diver.flinkdemo.entity.xxt.LastLocation;
import com.diver.flinkdemo.mapper.dorisMapper.LastLocationMapper;
import com.diver.flinkdemo.service.ILastLocationService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

//@Service
@Slf4j
public class LastLocationServiceImpl implements ILastLocationService {
//    @Autowired
    LastLocationMapper mapper ;
    SqlSession session = null;
    @Override
    public void saveLastLocation(LastLocation lastLocation) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession(true);
            mapper = session.getMapper(LastLocationMapper.class);
            mapper.insert(lastLocation);
        } finally {
            endOfSession(session);
        }

    }

    @Override
    public void saveNetworkStatus(String studentCardId, String networkStatus) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession();
            mapper = session.getMapper(LastLocationMapper.class);
            QueryWrapper<LastLocation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("imei", studentCardId);
            List<LastLocation> lls = mapper.selectList(queryWrapper);
            if (null != lls && !lls.isEmpty()) {
                LastLocation lastLocation = new LastLocation();
                lastLocation.setNetStatus(networkStatus);
                mapper.updateById(lastLocation);
            }
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public List<LastLocation> queryLastLocation(List<String> schoolIds, Long startTime, Long endTime) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession();
            mapper = session.getMapper(LastLocationMapper.class);
            QueryWrapper<LastLocation> queryWrapper = new QueryWrapper<>();
            if (null != schoolIds && !schoolIds.isEmpty()){
                queryWrapper.in("school_id", schoolIds);
            }
            if (null !=startTime && startTime > 0 ){
                queryWrapper.ge("upload_time" , startTime);
            }
            if (null !=endTime && endTime > 0 ){
                queryWrapper.le("upload_time" , endTime);
            }
            return mapper.selectList(queryWrapper);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public List<LastLocation> getLatestInTimes(Long startTime, Long endTime, List<String> studentCardIdList) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession();
            mapper = session.getMapper(LastLocationMapper.class);
            return mapper.getLatestInTimes(startTime,endTime,studentCardIdList);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public List<Map<String, Object>> getLastLocationList(Long startTime, Long endTime, List<String> studentCardIdLists) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession();
            mapper = session.getMapper(LastLocationMapper.class);
            return mapper.getLastLocationList(startTime, endTime, studentCardIdLists);
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public LastLocation getOneByParams(QueryWrapper<LastLocation> wrapper) {
        try {
            session = DynamicDataSourceUtil.dorisSessionFactory.openSession();
            mapper = session.getMapper(LastLocationMapper.class);
            return mapper.selectOne(wrapper);
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();

        }
    }
}
